package com.defysoft.waukids.message;


import org.json.JSONObject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.defysoft.waukids.BaseActivity;
import com.defysoft.waukids.Data_Sharedpreferences;
import com.defysoft.waukids.JsonParser;
import com.defysoft.waukids.PublicSharedpreferences;
import com.defysoft.waukids.R;
import com.defysoft.waukids.Sqlite_Connect;

public class Message_Detail_Activity extends BaseActivity{
	String PK_Mes_Code="";
	String Mes_Category="";
	String Mem_Rank="";
	Toolbar toolbar;
	TextView toolbartitle;
	View btn_send;
	String type[] = new String[7];
	TextView title [] = new TextView[7];
	TextView contents [] = new TextView[7];
	TextView date,name,scontents;
	com.defysoft.waukids.Custom_Data_Type.WK_MESSAGE message;
	Sqlite_Connect sc;
	PublicSharedpreferences psf;
	Data_Sharedpreferences.SF_Member member;
	public Sqlite_Connect sqlite_Connect;
	private final int SUCCESS = 10;
	private final int FAIL = 9;
	private final int NETWORK_ERR = 8;
	private final int SUCCESS_READ=7;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.message_detail_activity);
		psf = new PublicSharedpreferences(this);
		psf.setMembersp();
		member = psf.getMembersp();		
		Mem_Rank = member.Rank;		
		setStartData();
		setStartToolbar();
		startNetwork(0);
		registerReceiver(mToastMessageReceiver, new IntentFilter(
				"com.defysoft.waukids.TOAST_MESSAGE"));
	}
	private void setStartToolbar() {
		toolbar.setNavigationOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	void select_category(){		
		if (Mes_Category.equals("1")) {
			toolbartitle.setText("투약 의뢰서");
			type[0] = "아이";
			type[1] = "아이증상";
			type[2] = "약의 종류";
			type[3] = "투약일자";
			type[4] = "투약량";
			type[5] = "투약빈도";
			type[6] = "특이사항";
			scontents.setText("위와 같이 투약의뢰를 부탁드리며\n투약으로 인한 책임에 동의합니다.");
		}
		if (Mes_Category.equals("2")) {
			toolbartitle.setText("귀가 동의서");
			type[0]= "아이";
			type[1]= "귀가방법";
			type[2]= "요청시간";
			type[3]= "보호자";
			type[4]= "비상연락";
			type[5]= "동의기간";
			type[6]= "특이사항";
			scontents.setText("위와 같이 아이의 귀가를 부탁드리며\n사전에 준비를 모두 하겠습니다.");
		}
		for (int i = 0; i < title.length; i++) {
			title[i].setText(type[i]);
		}
	}
	
	@Override
	public void setStartData() {
		super.setStartData();
		sqlite_Connect = new Sqlite_Connect(this);
		PK_Mes_Code = getIntent().getStringExtra("PK_Mes_Code");
		Mes_Category = getIntent().getStringExtra("Mes_Category");

		btn_send = (View)findViewById(R.id.MESSAGE_WRITE_TOOLBAR_SAVE);
		toolbar = (Toolbar)findViewById(R.id.MESSAGE_DETAIL_TOOLBAR);
		date = (TextView)findViewById(R.id.MESSAGE_DETAIL_DATE);
		name = (TextView)findViewById(R.id.MESSAGE_DETAIL_NAME);
		scontents = (TextView)findViewById(R.id.MESSAGE_DETAIL_SENDCONTENTS);
		toolbartitle = (TextView) findViewById(R.id.MESSAGE_DETAIL_TOOLBAR_TITLE);
		title[0] = (TextView)findViewById(R.id.MESSAGE_DETAIL_TYPEONE_TITLE);
		title[1] = (TextView)findViewById(R.id.MESSAGE_DETAIL_TYPETWO_TITLE);
		title[2] = (TextView)findViewById(R.id.MESSAGE_DETAIL_TYPETHREE_TITLE);
		title[3] = (TextView)findViewById(R.id.MESSAGE_DETAIL_TYPEFOUR_TITLE);
		title[4] = (TextView)findViewById(R.id.MESSAGE_DETAIL_TYPEFIVE_TITLE);
		title[5] = (TextView)findViewById(R.id.MESSAGE_DETAIL_TYPESIX_TITLE);
		title[6] = (TextView)findViewById(R.id.MESSAGE_DETAIL_TYPESEVEN_TITLE);
		
		contents[0]= (TextView)findViewById(R.id.MESSAGE_DETAIL_TYPEONE_CONTENTS);
		contents[1]= (TextView)findViewById(R.id.MESSAGE_DETAIL_TYPETWO_CONTENTS);
		contents[2]= (TextView)findViewById(R.id.MESSAGE_DETAIL_TYPETHREE_CONTENTS);
		contents[3]= (TextView)findViewById(R.id.MESSAGE_DETAIL_TYPEFOUR_CONTENTS);
		contents[4]= (TextView)findViewById(R.id.MESSAGE_DETAIL_TYPEFIVE_CONTENTS);
		contents[5]= (TextView)findViewById(R.id.MESSAGE_DETAIL_TYPESIX_CONTENTS);
		contents[6]= (TextView)findViewById(R.id.MESSAGE_DETAIL_TYPESEVEN_CONTENTS);
		
		/**카테고리에 따라 제목 세팅*/
		select_category();
/*		if (Mem_Rank.equals("2")) {
			btn_send.setVisibility(View.VISIBLE);
		}*/
		btn_send.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startNetwork(1);
			}
		});
		
		sc = new Sqlite_Connect(this);
		sc.delete_New_Mas(Integer.parseInt(PK_Mes_Code));
	}
	
	void mainThread( int cho){
		try {
			JSONObject jsonObject;
			if(cho==0){
				jsonObject = new JSONObject(publicHttpCallback(HTTP_URL,
						"code=" + CODE_MESSAGE_DETAIL_LOAD+"&PK_Mes_Code="+PK_Mes_Code));
			}else{
				jsonObject = new JSONObject(publicHttpCallback(HTTP_URL,
						"code=" + CODE_MESSAGE_DETAIL_READ+"&PK_Mes_Code="+PK_Mes_Code));
			}			
			if (jsonObject.getBoolean("success")) {
				if(cho==0){
				onNetworkCallback(SUCCESS, jsonObject.getString("payload"));
				}else{
					onNetworkCallback(SUCCESS_READ, jsonObject.getString("payload"));
				}
			} else {
				onNetworkCallback(FAIL, jsonObject.getString("message"));
			}
		} catch (Exception e) {
			onNetworkCallback(NETWORK_ERR,e.toString()+" where getError()");
		}
		
	}
	
	@Override
	public void startThread(int choice) {
		super.startThread(choice);
		final int cho = choice;
		firstThread = new Thread(new Runnable() {

			@Override
			public void run() {
				switch (cho) {
				case 0:
					mainThread(0);
					break;

				case 1:
					mainThread(1);
					break;
					
				default:
					break;
				}

			}
		});
		firstThread.start();
		startDIal();
	}
	
	@Override
	protected void handlingMessage(Message _message) {
		super.handlingMessage(_message);
		switch (_message.what) {
		case SUCCESS:
			if (firstThread != null) {
				firstThread.interrupt();
			}
			message = JsonParser.message_data((String)_message.obj);
			contents[0].setText(message.Stu_Name);
			contents[1].setText(message.Mes_TypeOne);
			contents[2].setText(message.Mes_TypeTwo);
			contents[3].setText(message.Mes_TypeThree);
			contents[4].setText(message.Mes_TypeFour);
			contents[5].setText(message.Mes_TypeFive);
			contents[6].setText(message.Mes_TypeSix);
			date.setText(message.Mes_Date);			
			name.setText(message.Stu_Name+" 어린이 보호자 "+message.Mes_Writer);
			if(message.Mes_Check.equals("0") && Mem_Rank.equals("2") ){
				btn_send.setVisibility(View.VISIBLE);
			}
			endDIal();
			break;
		case SUCCESS_READ:
			if (firstThread != null) {
				firstThread.interrupt();
			}
			btn_send.setVisibility(View.INVISIBLE);
			endDIal();
			setResult(REFRESH_CODE);
			break;
			
		case FAIL:
			if (firstThread != null) {
				firstThread.interrupt();
			}
			Toast.makeText(this,(String)_message.obj, Toast.LENGTH_SHORT).show();
			endDIal();
			break;
			
		case NETWORK_ERR:
			if (firstThread != null) {
				firstThread.interrupt();
			}
			endDIal();
			sqlite_Connect.insert_Err_Log((String)_message.obj,android.os.Build.VERSION.SDK_INT,"Message_Detail_Activity", member.Email);
			break;
		}
	}
	
	private final BroadcastReceiver mToastMessageReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			LinearLayout layout = (LinearLayout)getLayoutInflater().inflate(R.layout.toast_layout, null);
			TextView toast_tv = (TextView)layout.findViewById(R.id.TOAST_LAYOUT_TV);
			Toast toast = new Toast(Message_Detail_Activity.this);
			toast.setView(layout);
			toast_tv.setText(intent.getStringExtra("PUSH_NAME")+" : "+intent.getStringExtra("PUSH_SUB"));
			toast.setDuration(Toast.LENGTH_LONG);
			toast.show();
		}
	};

}
